﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class Longin : Form
    {
        string tenTaikhoan = "";
        string matKhau = "";
        private SqlConnection con;
        private SqlDataAdapter da;
        public Longin()
        {
            InitializeComponent();
            tbtendn.Text = "nquang";
            tbmk.Text = "123456";
        }
        public string GetAccount()
        {
            tenTaikhoan = tbtendn.Text;
            return tenTaikhoan;
        }
        public string GetPassword()
        {
            matKhau = tbmk.Text;
            return matKhau;
        }
        private void connect()
        {
            string cn = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";

            try
            {
                con = new SqlConnection(cn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the ket noi co so du lieu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Boolean testlongin(string tentaikhoan, string matkhau)
        {
            connect();
            SqlDataReader rdr = null;
            string sql = "SELECT NhanVien.TenTaiKhoan, TaiKhoan.MatKhau FROM NhanVien, TaiKhoan where NhanVien.TenTaiKhoan = TaiKhoan.TenTaiKhoan";
            try
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    if (tentaikhoan.Equals(rdr[0].ToString().Trim()) && matkhau.Equals(rdr[1].ToString().Trim())) return true;
                }

            }
            catch (Exception)
            {
                MessageBox.Show("loi");
            }
            return false;
        }

        private void btndangnhap_Click(object sender, EventArgs e)
        {
            if (testlongin(tbtendn.Text, tbmk.Text))
            {
                this.Hide();
                new Frmmain().Show();
            }
            else MessageBox.Show("Đăng nhập thất bại");
        }

        private void Longin_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void btnHuyBo_Click(object sender, EventArgs ev)
        {
            Application.Exit();
        }
    }
}
